package com.kang.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kang.pojo.IntemInformation;
import com.kang.service.AuditService;
import com.kang.service.InventoryService;

/**
 * 主页面控制层
 * 
 * @author zs614
 *
 */
@Controller
public class IndexController extends com.kang.controller.Controller {
	/**
	 * 库存服务层
	 */
	@Resource
	private InventoryService inventoryService;
	/**
	 * 审核服务层
	 * 
	 */
	@Resource
	private AuditService auditService;

	/**
	 * 首页导航页面
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/index")
	public String index(ModelMap model, HttpServletRequest request) {
		Integer userID = (Integer) request.getSession().getAttribute("userID");
		if (userID == null) {
			Cookie cookie = null;
			if (cookieMap != null) {
				cookie = cookieMap.get("meterial_manage_username");
			}
			String loginAccount = "";
			if (cookie != null)
				loginAccount = cookie.getValue();
			model.addAttribute("meterial_manage_username", loginAccount);
			return "basic/login";
		}
		return "basic/wzgl";
	}

	/**
	 * 首页导航页面
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("")
	public String index2(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		Integer userID = (Integer) request.getSession().getAttribute("userID");
		if (userID == null) {
			readCookieMap(request);
			Cookie cookie = cookieMap.get("meterial_manage_username");
			String loginAccount = "";
			if (cookie != null)
				loginAccount = cookie.getValue();
			model.addAttribute("meterial_manage_username", loginAccount);
			return "basic/login";
		}
		return "basic/wzgl";
	}

	/**
	 * 退出登录
	 * 
	 * @param session
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/logout")
	public void index(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		session.removeAttribute("userName");
		session.removeAttribute("list");
		session.removeAttribute("userID");
		session.removeAttribute("modules");
		response.sendRedirect(request.getContextPath() + "/index");
	}

	/**
	 * 桌面，查看个人任务
	 * 
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/desktop")
	public String desktop(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model)
			throws Exception {
		initRight(session);

		// 待我审批
		Map<String, String> mapzz = auditService.getdatarRecoardnumber(userID);
		model.addAttribute("mapzz", mapzz);

		// 库存预警
		model.addAttribute("isInventoryWarning", checkRight("库存预警") == SUCCESS);
		if (checkRight("库存预警") == SUCCESS) {
			List<IntemInformation> inventoryWarningList = inventoryService.getDesktopInventoryWarningList();
			model.addAttribute("inventoryWarningList", inventoryWarningList);
		}

		// 有效期预警
		model.addAttribute("isValidityWarning", checkRight("有效期预警") == SUCCESS);
		if (checkRight("有效期预警") == SUCCESS) {
			List<IntemInformation> validityWarningList = inventoryService.getDesktopValidityWarningList();
			model.addAttribute("validityWarningList", validityWarningList);
		}

		return "basic/welcome";
	}

	/**
	 * 判断未登录，跳转此接口
	 * 
	 * @return 返回未登录jsp页面
	 */
	@RequestMapping("notLogin")
	public String notLogin() {
		return "basic/loginerror";
	}

}
